<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/24 14:00:35 $)
// $RCSfile: functions_irs.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->loadLanguageSection("tickets");
require_once ("./modules/tickets/functions_search.php");
require_once ("./modules/tickets/global_searchstopwords.php");

/**
* Run an IRS Search and return the list of items
*/
function _runIRS($query, $isstaffcp = false)
{
	global $module, $_SWIFT, $dbCore;

	if (empty($query) || !$module->isRegistered(MODULE_TICKETS))
	{
		exit;
	}

	$_worddata = returnSanitizedText($query);
	$_wordlist = explode(" ", $_worddata);
	$_finalwordlist = array();
	$_finalworddata = "";

	$wordcount = 0;
	foreach ($_wordlist as $key=>$val)
	{
		if ($wordcount > intval($_SWIFT["settings"]["t_maxwcnt"]))
		{
			break;
		}

		if (shouldIndexWord($val))
		{
			$_finalwordlist[] = $val;
			$_finalworddata .= $val." ";
			$wordcount++;
		}
	}

	if (count($_finalwordlist))
	{
		$_finalworddata = trim($_finalworddata);

		$_kbarticleidlist = $_kbarticlelist = $_relevance = $_relevancepointer = array();
		if($isstaffcp != true)
		{
			$dbCore->query("SELECT kbarticlelinks.kbarticleid, MATCH(kbarticledata.contents) AGAINST ('". $dbCore->escape($_finalworddata) ."') AS relevance FROM `". TABLE_PREFIX ."kbarticledata` AS kbarticledata LEFT JOIN `". TABLE_PREFIX ."kbarticlelinks` AS kbarticlelinks ON (kbarticledata.kbarticleid = kbarticlelinks.kbarticleid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbarticlelinks.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."') LEFT JOIN `swkbcategories` AS kbcategories ON (kbarticlelinks.kbcategoryid = kbcategories.kbcategoryid) WHERE (tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' OR kbarticlelinks.kbcategoryid = '0') AND MATCH(kbarticledata.contents) AGAINST('". $dbCore->escape($_finalworddata) ."') LIMIT 5;");
		}else
		{
			$dbCore->query("SELECT kbarticlelinks.kbarticleid, MATCH(kbarticledata.contents) AGAINST ('". $dbCore->escape($_finalworddata) ."') AS relevance FROM `". TABLE_PREFIX ."kbarticledata` AS kbarticledata LEFT JOIN `". TABLE_PREFIX ."kbarticlelinks` AS kbarticlelinks ON (kbarticledata.kbarticleid = kbarticlelinks.kbarticleid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbarticlelinks.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."') WHERE MATCH(kbarticledata.contents) AGAINST('". $dbCore->escape($_finalworddata) ."') LIMIT 5;");
		}
		while ($dbCore->nextRecord())
		{
			$_kbarticleidlist[] = $dbCore->Record["kbarticleid"];
			$_relevance[] = $dbCore->Record["relevance"];
			$_relevancepointer[$dbCore->Record["kbarticleid"]] = $dbCore->Record["relevance"];
		}

		if (count($_kbarticleidlist))
		{
			$_sortpointer = array();
			$maxrel = max($_relevance);
			$minrel = min($_relevance);

			$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."kbarticles` AS kbarticles LEFT JOIN `". TABLE_PREFIX ."kbarticledata` AS kbarticledata ON (kbarticles.kbarticleid = kbarticledata.kbarticleid) WHERE kbarticles.kbarticleid IN (". buildIN($_kbarticleidlist) .");");
			while ($dbCore->nextRecord())
			{
				if ((!$isstaffcp && $dbCore->Record["articlestatus"] == ARTICLE_PUBLISHED) || $isstaffcp == true) // Make sure we only display the published articles
				{
					$_relevance = $_relevancepointer[$dbCore->Record["kbarticleid"]];

					if ($maxrel != 0) {
						$_articlerel = ($_relevance/$maxrel)*100;
					} else {
						$_articlerel = 0;
					}

					$_sortpointer[$dbCore->Record["kbarticleid"]] = $_articlerel;

					$_kbarticlelist[$dbCore->Record["kbarticleid"]] = $dbCore->Record;
					$_kbarticlelist[$dbCore->Record["kbarticleid"]]["contents"] = strip_tags(substr($dbCore->Record["contents"], 0, $_SWIFT["settings"]["kb_climit"]));
					$_kbarticlelist[$dbCore->Record["kbarticleid"]]["relevance"] = number_format($_articlerel, 2);
				}
			}

			arsort($_sortpointer);

			$_finalkbarticlelist = array();
			foreach ($_sortpointer as $key=>$val)
			{
				$_finalkbarticlelist[$key] = $_kbarticlelist[$key];
			}

			return $_finalkbarticlelist;
		}
	}

	return false;
}
?>